package com.kuqi.crs.workbench.service.Impl;

import com.kuqi.crs.settings.dao.UserDao;
import com.kuqi.crs.settings.domain.User;
import com.kuqi.crs.util.SqlSessionUtil;
import com.kuqi.crs.vo.PaginationVO;
import com.kuqi.crs.workbench.dao.ActivityDao;
import com.kuqi.crs.workbench.dao.ActivityRemarkDao;
import com.kuqi.crs.workbench.domain.Activity;
import com.kuqi.crs.workbench.domain.ActivityRemark;
import com.kuqi.crs.workbench.service.ActivityService;

import java.util.List;
import java.util.Map;

/**
 * @author:kuqi
 * @createTime:2021/9/24 21:47
 * @description:
 */
public class ActivityServiceImpl implements ActivityService{
    //使用动态代理获取ActivityDao的实例对象,传李四，获取张三
    ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    ActivityRemarkDao activityRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);


    public boolean save(Activity a) {
        boolean flag = true;
        int count = activityDao.save(a);
        if (count != 1){
            flag  = false;
        }
        return flag;
    }

    public PaginationVO<Activity> dataList(Map<String, Object> map) {
        //获取total总列表数
        int total = activityDao.getTotalByCondition(map);

        //获取dataList市场活动信息列表
        List<Activity> dataList = activityDao.getDataListByCondition(map);

        //将total总数与dataList封装到PaginationVo实体类中
        PaginationVO<Activity> vo = new PaginationVO<Activity>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        return vo;
    }

    public boolean delete(String[] ids) {
        boolean flag = true;
        //查询activity备注表中需要删除的数量
        int count1 = activityRemarkDao.selectCountById(ids);

        //删除activity备注表中的数据返回int
        int count2 = activityRemarkDao.deleteRemarkById(ids);
        //判断查询到的与删除的记录是否相等
        if (count1!=count2){
            flag = false;
        }

        //删除activity表中的数据
        int count3 = activityDao.deleteActivityById(ids);

        //判断删除activity表中的记录是否等于ids
        if (count3 != ids.length){
            flag = false;
        }

        return flag;
    }

    public PaginationVO<User> getUserListAndActivityList(String id) {
        PaginationVO<User> vo = new PaginationVO<User>();
        //获取用户列表
        List<User> userList = userDao.getUserList();

        vo.setDataList(userList);
        //获取商品信息
        Activity activity = activityDao.getActivity(id);
        vo.setActivity(activity);

        return vo;
    }

    public boolean update(Map<String, Object> map) {
        boolean flag = true;
        int result = activityDao.update(map);
        if (result==0){
            flag = false;
        }
        return flag;
    }

    public Activity detail(String id) {
        //获取市场活动信息
        Activity activity = activityDao.getDetail(id);
        return activity;
    }

    public List<ActivityRemark> getActivityListById(String activityId) {
        List<ActivityRemark> arList = activityRemarkDao.getActivityListById(activityId);
        return arList;
    }

    public boolean deleteRemark(String id) {
        boolean flag = true;
        int count = activityRemarkDao.deleteRemark(id);
        if (count!=1){
            flag = false;
        }
        return flag;
    }

    public boolean saveRemark(ActivityRemark ar) {
        boolean flag = true;
        int count = activityRemarkDao.saveRemark(ar);
        if (count!=1){
            flag = false;
        }
        return flag;
    }

    public boolean updateRemark(ActivityRemark ar) {
        boolean flag = true;
        int count = activityRemarkDao.updateRemark(ar);
        if (count != 1)
        {
            flag = false;
        }
        return flag;
    }

}
